package com.zh.test.fastdfs;

import com.zh.test.bean.OutJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author zh
 * @Description  测试文件上传controller
 * @Date 2020/9/1
 * @Param
 * @return
 **/
@RestController
@RequestMapping(value = "/file")
@Slf4j
public class FileController {
    @Autowired
    fastdfsUtil fastdfsUtil;
    @Autowired
    fastdfsService fastdfsService;

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public OutJson uploadFile(@RequestParam("file") MultipartFile file){
        String filePath = null;
        try {
            filePath =  fastdfsUtil.upload(file);
        }catch (Exception e){
            log.error("文件上传失败");
            e.printStackTrace();
        }
        return  new OutJson().setCode(200).setMessage("success").setObject(filePath);
    }


    @PostMapping(value = "uploadPath")
    public OutJson uploadInstream(@RequestParam(required = false) String oldfilePath){
        String s = fastdfsService.uploadFromPath(oldfilePath);
        return  new OutJson().setCode(200).setMessage("success").setObject(s);
    }
}
